package com.rzico.sbl.ui.report.manager

import android.widget.ImageView
import androidx.core.view.isVisible
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportDriverBinding
import com.rzico.sbl.model.ManagerStation
import com.rzico.sbl.other.getEndDate
import com.rzico.sbl.other.getFirstDate
import com.rzico.sbl.ui.report.other.ReportUrl
import com.rzico.sbl.viewmodel.ReportDriverViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.extend.orEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.*
import com.xinnuo.common_ui.glideExt.loadCircleImage
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONObject

class ReportDriverActivity : BaseActivity(R.layout.activity_report_driver) {

    private val mBinding by lazy { ActivityReportDriverBinding.bind(getRootView()) }

    override fun getViewModel() = get<ReportDriverViewModel>()

    override fun initData() {
        setToolbarVisibility(false)
        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getTotalData()
        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关司机信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = {
                        getTotalData()
                        getData(0)
                    },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                mAdapter = SlimAdapter.creator()
                    .register<ManagerStation>(R.layout.item_report_driver) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_driver_name, bean.linkman)
                            text(R.id.item_driver_phone, bean.phone)
                            text(R.id.item_driver_num, bean.subQuantity)

                            with<ImageView>(R.id.item_driver_img) {
                                it.loadCircleImage(bean.logo, R.mipmap.default_logo3)
                            }

                            clicked {
                                startActivityEx<ReportDriverDetailActivity>(
                                    "memberId" to bean.member,
                                    "logo" to bean.logo.orEmpty(),
                                    "name" to bean.linkman.orEmpty(),
                                    "phone" to bean.phone.orEmpty(),
                                    "count" to bean.subQuantity.orEmpty()
                                )
                            }
                        }
                    }
                    .attachTo(view)
            }
        }
    }

    /**
     * 获取本月数据
     */
    private fun getTotalData() {
        getViewModel()
            .request(
                isLoadingBefore = false,
                isLoadingAfter = false,
                requestUrl = ReportUrl.getBaseUrl().zDrisum,
                requestMethod = HttpMethod.GET,
                params = arrayOf(
                    "beginDate" to getFirstDate(),
                    "endDate" to getEndDate()
                ),
                onSuccess = {
                    val obj = JSONObject(it)

                    mBinding.apply {
                        driverTotal.text = obj.optNotNull("quantity")
                        driverAmount.text = obj.optNotNull("subQuantity")
                    }
                }
            )
    }

    /**
     * 司机列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .driverList(
                page = index,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}